package com.ailiwu.www.ailiwu.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.activity.ClasscifyGridItemActivity;
import com.ailiwu.www.ailiwu.bean.ClassifyChannelsRecyc;
import com.ailiwu.www.ailiwu.url.Urls;
import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zzw on 2015/12/4.
 */
public class StrategyGridAdapter extends RecyclerView.Adapter<StrategyGridAdapter.ViewHolder> {
    private LayoutInflater mInflater;
    private ClassifyChannelsRecyc mCftr;
    private List<ClassifyChannelsRecyc.DataEntity.ChannelGroupsEntity.ChannelsEntity> collections;
    private static Context mContext;
    private BitmapUtils bitType;

    /**
     *
     * @param context
     * @param datas  传递过来的Json数据对象
     * @param type  不同的模块的类型例如：0=品类、1=对象...
     */
    public StrategyGridAdapter(Context context, Object datas, int type) {
        mInflater = LayoutInflater.from(context);

        mContext = context;

        collections = new ArrayList<>();
        if (datas != null) {
            mCftr = (ClassifyChannelsRecyc) datas;
            collections = mCftr.getData().getChannel_groups().get(type).getChannels();
            bitType = new BitmapUtils(mContext);
//			net.display(iv, "http://bbs.lidroid.com/static/image/common/logo.png");
            //设置是否支持本地缓存
            bitType.configDiskCacheEnabled(true);
            //设置是否支持内存缓存
            bitType.configMemoryCacheEnabled(true);
            //设置图片的最大宽高
            bitType.configDefaultBitmapMaxSize(60, 60);
            //设置图片的默认颜色处理方式
            bitType.configDefaultBitmapConfig(Bitmap.Config.ARGB_8888);
            //设置缓存的有效期
            bitType.configDefaultCacheExpiry(24*3600*1000);
            //设置联网读取图片的超时时间
            bitType.configDefaultConnectTimeout(20000);
            //设置图片的默认BitmapDisplayConfig对象
//			net.configDefaultDisplayConfig(displayConfig)
            //设置本地图片读取的超时时间
//			net.configDefaultReadTimeout(readTimeout);
            //设置加载失败显示的图片
//			net.configDefaultLoadFailedImage(bitmap);
            //设置加载过程中显示的图片
//			net.configDefaultLoadingImage(bitmap);
            //设置连接线程数
//			net.configThreadPoolSize(threadPoolSize);

        }




    }


    @Override
    public int getItemCount() {
        return collections == null ? 0 : collections.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item_type_strategy,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);


        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        //绑定显示的图片
        bitType.display(viewHolder.mImg, collections.get(i).getIcon_url());
        //绑定显示的标题
        viewHolder.tvTitle.setText(collections.get(i).getName());

        //每项设置监听
        viewHolder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ClasscifyGridItemActivity.class);
                intent.putExtra("urlID", Urls.strategyChannelGroupsUrl(collections.get(i).getId()));
                intent.putExtra("title",collections.get(i).getName());
                mContext.startActivity(intent);
            }
        });

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView mImg;
        TextView tvTitle;
        View view;
        public ViewHolder(View view) {
            super(view);
            this.view = view;
            mImg = (ImageView) view
                    .findViewById(R.id.iv_type_startegy);

            tvTitle = (TextView) view.findViewById(R.id.tv_type_startegy);

        }

    }
}